package modulesAPI.v2.composite;
import java.util.LinkedList;

import modulesAPI.v2.Module;
import modulesAPI.v2.ViewType;
import modulesAPI.v2.basics.ModCalendar;
import modulesAPI.v2.basics.ModTextArea;
import budget.Service;

import com.caterina.components.CPanel;
import com.caterina.resources.CModules;
import com.caterina.resources.CWords;

import contract.EventContract;

public class MEventContract extends Module<EventContract>{

	public MEventContract(Module<?> ownerModule, EventContract content, String tag, String description, ViewType viewType,int... options) {
		super(ownerModule, content, tag, description, viewType,options);
		createModules();
		this.addModules(budget, stipulationDate, validityDate, clauses);
	}
	
	private void createModules(){
		ViewType vt= this.getViewType();
		if (this.getContent().isLocked())vt=ViewType.READ_ONLY;
		budget=new MBudget(this, this.getContent().getBudget(), new LinkedList<Service>(),"BUDGET","BUDGET_DESC",ViewType.READ_ONLY);
		stipulationDate = new ModCalendar(this, this.getContent().getStipulationDate(),"CONTRACT_STIPULATION_DATE","CONTRACT_STIPULATION_DATE_DESC", ViewType.READ_ONLY);
		validityDate = new ModCalendar(this, this.getContent().getValidityDate(),"CONTRACT_VALIDITY_DATE","CONTRACT_VALIDITY_DATE_DESC", ViewType.READ_ONLY);
		clauses = new ModTextArea(this, this.getContent().getClauses(),"CONTRACT_CLAUSES","CONTRACT_CLAUSES_DESC", vt);
	}
	@Override
	protected CPanel getReadOnlyPanel(boolean isSelected) {
		return this.getDefaultPanel();
	}
	@Override
	protected CPanel getWritablePanel(boolean isSelected) {
		return this.getDefaultPanel();
	}
	@Override
	public CPanel getInListPanel(boolean isSelected) {
		return CModules.formatForList(budget.getContent().getRequest().getEvent().getEventName() + "  " + CWords.getWord(budget.getContent().getRequest().getEvent().getEventType().name()), 
				budget.getContent().getRequest().getCustomer().getName()+" "+budget.getContent().getRequest().getCustomer().getSurname()+ " tot: "+ budget.getContent().getTotal(), isSelected);
	}
	@Override
	protected void beforeFireChange(Module<?> mod) {
		this.setContent(new EventContract(
				this.getContent().getId(), 
				stipulationDate.getContent(),
				validityDate.getContent(),
				clauses.getContent(),
				budget.getContent()
				));
	}
	@Override
	protected CPanel getOneLinePanel(boolean isSelected) {
		return null;
	}
	@Override
	protected void afterFireChange(Module<?> mod) {	}
	private MBudget budget;
	private ModCalendar stipulationDate;
	private ModCalendar validityDate;
	private ModTextArea clauses;
}
